/**
 * Created by honkey on 16-4-20.
 * description 单品推荐
 */
$(function(){
    var data = {};
    var pageIndex = 1;	//页码默认为第一页

    $('.good-cat').on('change' , function(){
        var value = $(this).val();
       if (value != '') {
           var data = {};
           data.parentId = value;
           $.ajax({
               url : '/index.php?m=Home&c=Activity%2FCreateAdvert&a=ajaxGetGoodCat',
               data : data,
               type : 'POST',
               dataType : 'JSON',
               success : function(data){
                   if (data.success == true) {
                        var data = data.data;
                       if (data.length > 1) {
                            setCat(data);
                       } else {
                           $('.good-cat-two').hide();
                           alert('暂无数据');
                       }
                   } else {
                       alert(data.message);
                   }

               }
           })
       } else {
           $('.good-cat-two').hide().html("")
       }
    });
    function setCat(param){
        var html = '<option value="">请选择</option>';
        for (var i = 0; i < param.length; i++) {
                html +='<option value="'+ param[i].id +'">'+ param[i].name +'</option>';
        }
        $('.good-cat-two').show().html(html);
    }

    // 搜索条件
    $('.btn-search').on('click',function(){
        var name = $('.shopName').val(); // 商品名称
        var sn  = $('.shopSn').val(); // 商品货号
        var minprice =$('.minprice').val(); // 最小价格
        var maxprice = $('.maxprice').val(); // 最大价格
        var catfirst = $('.good-cat').val(); // 一级分类
        var cattwo = $('.good-cat-two').val(); // 二级分类
        var cat = (cattwo != '' && cattwo !=null ) ? cattwo : catfirst;


        if (minprice != '' && (typeof parseFloat(minprice == 'NaN') ) ) {
            alert("价格输入错误！");
            return false;
        } else if (maxprice != '' && (typeof parseFloat(maxprice == 'NaN') ) ) {
            alert("价格输入错误！");
            return false;
        } else if ((name == '') && (sn == '') && (minprice == '') && (maxprice == '') && (cat == '')){
            alert("请输入搜索条件！");
            return false;
        }
        data.name= name;
        data.sn= sn;
        data.firstPrice= minprice;
        data.lastPrice= maxprice;
        data.catId= cat;
        getGoods(data);
    });

    // 翻页
    function setPage(num){
        $('#page').find('a').on('click',function(e){
            e.preventDefault();
            var that = $(this);
            var parent =that.parent();
            if (parent.hasClass("next")) {
                if (pageIndex == num) {
                    return false;
                } else {
                    pageIndex++;
                }
            } else if (parent.hasClass("prev")){
                if (pageIndex == 1) {
                    return false;
                } else {
                    if (that.text() == 'First') {
                        pageIndex = 1;
                    } else{
                        pageIndex --;
                    }
                }
            } else {
                pageIndex = parseInt(that.text());
            }

            data.p = pageIndex;
            getGoods(data) ;
        });
    }


    // 请求商品数据
    function getGoods(data){
        $.ajax({
            url : '/index.php?m=Home&c=Activity%2FCreateAdvert&a=ajaxGetSinglegood',
            data : data,
            type : 'GET',
            dataType : 'JSON',
            success : function(data){
                if (data.success == true) {
                    if (!$.isArray(data.data)){
                        var pagehtml = data.data.pageHtml;
                        var num = Math.ceil(parseInt(data.data.totalNum)/5);
                        var data = data.data.list;

                        goodRender(data,pagehtml,num);
                    } else {
                        $('.good-content').children().remove();
                        $('#page').children().remove();
                    }

                } else {
                    alert(data.message);
                }
            }

        });

    }

    getGoods();

    // 拼装页面
    function goodRender(param,pageHtml,num){
        var html = '';
        var goodArray = [];
        $('.select-content').find('input').each(function() {
            goodArray.push($(this).data().id);
        });
        for (var i = 0;i < param.length;i++) {
            for (var j = 0; j < goodArray.length; j++) {
                if (param[i].id == goodArray[j]) {
                    delete param[i].id;
                };
            };
            if ( param[i].id !=undefined) {
                html +='<tr align="center">\
                        <td><input type="checkbox" data-id = ' + param[i].id + '></td>\
                        <td>' + param[i].id + '</td>\
                        <td>' + param[i].name + '</td>\
                        <td>' + param[i].sn + '</td>\
                        <td>' + param[i].price + '</td>\
                        <td>' + param[i].stock + '</td>\
                    </tr>';
            }

        }

        $('.good-content').html(html);
        $('#page').html(pageHtml);
        setPage(num);
    }

    // 添加商品
    $('.btn-setgood').on('click',function(){
        var goodArr = [];
        var tr = $('.good-content').find('input');
        tr.each(function(){
            if ($(this).prop("checked")){
                goodArr.push($(this).parents('tr'));
            }
        });
        if (goodArr.length > 0) {
            for(var i =0 ;i < goodArr.length;i++){
                $('.select-content').append(goodArr[i]);
            }
            $('.select-content input').prop("checked",false);
        } else {
            alert('请选择商品');
        }
    });

    // 删除商品
    $('.btn-deletegood').on('click',function(){
        var tr =  $('.select-content tr');
        if (tr.length > 0) {
            tr.each(function(){
                $(this).find("input").each(function(){
                    if ($(this).prop("checked")) {
                        $(this).parents("tr").remove();
                    };
                })
            });

        } else {
            alert('请选择要删除的商品');
            return false;
        }
    });

    // 点击提交
    $('.btn-full-complete').on('click',function(){
        var good = $('.select-content').find("input");
        var selectGoodArr = [];
        good.each(function(){
            selectGoodArr.push($(this).data().id);
        });
        $("input[name='goods']").val(selectGoodArr.toString());
        $('form').submit();

    });

    // 修改权重
    $('.input-sort-reset').each(function(){
        $(this).blur(function(){
            var linkid = $(this).parents('tr').data().id;
            var sort = $(this).val();
            var  data = {};
            if (sort != "" && parseInt(sort) != NaN ) {
                data.id = linkid;
                data.sort = sort;
                $.ajax({
                    url : '/index.php?m=Home&c=Activity%2FCreateAdvert&a=ajaxEditSingleGoodSort',
                    data : data,
                    type : 'POST',
                    dataType : 'JSON',
                    success : function(data){
                        if (data.success == true) {
                            alert(data.message);
                        } else {
                            alert(data.message);
                        }
                    }

                });
            } else {
                alert('权重不合法');
            }

        });
    });

    // 暂停或者开启
    $(".btn-status-reset").each(function(){
        $(this).on('click',function(){
            var that =  $(this);
            var linkid = that.parents('tr').data().id;
            var statusName = that.html();

            if (statusName == '开启'){
                var  status = 'start';
                fianlShow = '进行中';
                finalName = '暂停';

            } else {
                var  status = 'end';
                fianlShow = '已暂停';
                finalName = '开启';
            }
            data.id = linkid;
            data.status = status;
            $.ajax({
                url : '/index.php?m=Home&c=Activity%2FCreateAdvert&a=ajaxEditSingleGoodStaus',
                data : data,
                type : 'POST',
                dataType : 'JSON',
                success : function(result){
                    if (result.success == true) {
                        that.parents('tr').find('.featured-status').html(fianlShow);
                        that.text(finalName);
                        if (status == 'end') {
                            that.removeClass('blue').addClass('red');
                        } else {
                            that.removeClass('red').addClass('blue');
                        }
                        alert(result.message);
                    } else {
                        alert(result.message);
                    }
                }
            });
        });
    });

    // 删除
    $('.btn-single-delete').each(function(){
        $(this).on('click',function(){
            var parents = $(this).parents('tr');
            var linkid = parents.data().id;
            var status = 'delete';
            var data = {};
            $('.btn-submit').on('click',function(){
                data.id = linkid;
                data.status = status;
                $.ajax({
                    url : '/index.php?m=Home&c=Activity%2FCreateAdvert&a=ajaxEditSingleGoodStaus',
                    data : data,
                    type : 'POST',
                    dataType : 'JSON',
                    success : function(data){
                        if (data.success == true) {
                            parents.remove();
                           $('.close').trigger('click');
                        } else {
                            alert(data.message);
                        }
                    }

                });
            });
        });
    });
})